﻿using System;

namespace Kaleida.ServiceMonitor.Framework
{
	public class ScheduledTime : IEquatable<ScheduledTime>
	{
		private readonly int hour;
		private readonly int minute;

        public ScheduledTime(TimeSpan timeOfDay) : this(timeOfDay.Hours, timeOfDay.Minutes)
        {
        }

		public ScheduledTime(int hour, int minute)
		{
			if(hour < 0 || hour > 23) throw new ArgumentOutOfRangeException("hour", hour, "Must be between 0 and 23");
			if (minute < 0 || minute > 59) throw new ArgumentOutOfRangeException("minute", minute, "Must be between 0 and 59");
				
			this.hour = hour;
			this.minute = minute;
		}

		public int Hour
		{
			get { return hour; }
		}

		public int Minute
		{
			get { return minute; }
		}

		public bool Equals(ScheduledTime other)
		{
			if (ReferenceEquals(null, other)) return false;
			if (ReferenceEquals(this, other)) return true;
			return other.hour == hour && other.minute == minute;
		}

		public override bool Equals(object obj)
		{
			if (ReferenceEquals(null, obj)) return false;
			if (ReferenceEquals(this, obj)) return true;
			if (obj.GetType() != typeof (ScheduledTime)) return false;
			return Equals((ScheduledTime) obj);
		}

		public override int GetHashCode()
		{
			unchecked
			{
				return (hour*397) ^ minute;
			}
		}

		public static bool operator ==(ScheduledTime left, ScheduledTime right)
		{
			return Equals(left, right);
		}

		public static bool operator !=(ScheduledTime left, ScheduledTime right)
		{
			return !Equals(left, right);
		}

		public TimeSpan ToTimeSpan()
		{
			return new TimeSpan(hour, minute, 0);
		}

		public override string ToString()
		{
			return string.Format("{0:00}:{1:00}", hour, minute);
		}
	}
}
